package com.cq.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.cq.health.dao.OrderSettingDao;
import com.cq.health.pojo.OrderSetting;
import com.cq.health.service.OrderSettingService;
import com.sun.org.apache.xpath.internal.operations.Or;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Author: 悠久的苍穹
 * @Date: 2020/6/2 12:32
 * @Description: 预约设置业务实现接口
 * @Version: 1.0
 */
@Service
@Slf4j
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;
    @Transactional/*(rollbackFor = Exception.class)*/
    @Override
    public void add(List<OrderSetting> orderSettingList) {
        log.debug(">>>   orderSettingList:{}",orderSettingList);
        //遍历数据，插入或更新数据
        for (OrderSetting orderSetting : orderSettingList) {
            Long count = orderSettingDao.countByOrderDate(orderSetting.getOrderDate());
            if(count>0){
                //有数据
                orderSettingDao.update(orderSetting);
            }
            else {
                orderSettingDao.add(orderSetting);
            }

        }

    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        log.debug("getOrderSettingByMonth date:"+date);
        ArrayList<Map> list = new ArrayList<>();
        //格式{ date: 3, number: 100, reservations: 0 }
        String beginDate = date+"-1";
        String endDate = date+"-31";
        List<OrderSetting> orderSettingList = orderSettingDao.getOrderSettingByMonth(beginDate,endDate);
        for (OrderSetting orderSetting : orderSettingList) {
            Map one = new HashMap();
            one.put("date",orderSetting.getOrderDate().getDate());
            one.put("number",orderSetting.getNumber());
            one.put("reservations",orderSetting.getReservations());
            log.debug("orderSetting:"+orderSetting);
            list.add(one);
        }

        return list;
    }

    @Override
    public void editOrderSettingByDate(OrderSetting orderSetting) {
        log.debug(">>>editOrderSettingByDate orderSetting:"+orderSetting);
        Long count = orderSettingDao.countByOrderDate(orderSetting.getOrderDate());
        if(count>0){
            //有数据
            orderSettingDao.update(orderSetting);
        }
        else {
            orderSettingDao.add(orderSetting);
        }
    }
}
